package com.jhws.business.customer.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.jhws.business.customer.bo.SdkCustomers;
import com.jhws.sys.user.service.ITblUserDimService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;

import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;

@Service
public class SdkCustService {
	@Resource
	private BaseDao BaseDao;
	@Resource
	private ITblUserDimService userDimService;

	public void saveCustomer(SdkCustomers customers) {
	    if(StringUtils.isEmpty(customers.getId()))
	        customers.setId(null);
		BaseDao.saveOrUpdate(customers);
	}

	public Map queryCustomerList(Map param) {
		String where = " where 1=1 ";
		if(param.get("customName") != null)
		    where += "and custom_name like %'"+param.get("customName")+"'";
		return BaseDao.queryPage("from SdkCustomers "+where);
	}

	public List<SdkCustomers> queryCustomerList() {
	    return BaseDao.findList("from SdkCustomers where 1 = 1");
	}

	/**
	 * 删除资源
	 * 
	 * @param id
	 */
	public void deleteCustom(String id) {
		BaseDao.delete(SdkCustomers.class,id);
	}

	public SdkCustomers findCustomById(String id) {
		List<SdkCustomers> list = BaseDao.findList("from SdkCustomers where id = '"+id+"'");
		if(list != null && list.size()!=0)
			return list.get(0);
		else
			return null;
	}

	public SdkCustomers findCustomByCustomId(String customId) {
		List<SdkCustomers> list = BaseDao.findList("from SdkCustomers where customId = '"+customId+"'");
		if(list != null && list.size()!=0)
			return list.get(0);
		else
			return null;
	}

	public boolean isCustomNameExist(String customName) {
		String sql = "SELECT\n" 
				+ "   a.id,\n" 
				+ "   a.customName,\n" 
				+ "   a.customId,\n" 
				+ "   a.isEnable\n"
				+ " FROM\n" 
				+ "  customers_info a\n" 
				+ " WHERE\n" 
				+ "   a.customName ='"+customName+"'";
		List<SdkCustomers> cList = BaseDao.j().query(sql, new Object[]{}, new BeanPropertyRowMapper<SdkCustomers>(SdkCustomers.class));
		if (cList.size() > 0) {
			return true;
		}
		return false;
	}

	public boolean isPerms(String customId, String perms){
		List<String> permss =  userDimService.findUserPerms(customId);
		for (String per : permss){
			if (per.equals(perms)){
				return true;
			}
		}
		return false;
	}
}
